<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按钮</title>
    <style>
        button {
            padding: 10px 20px;
            background-color: gray;
            border: none;
            cursor: pointer;
        }

        button.dl {
            background-color: green;
            color: white;
        }
    </style>
</head>

<body>
    <button id="btn1">按钮1</button>
    <button id="btn2">按钮2</button>
    <button id="btn3">按钮3</button>
    <button id="btn4">按钮4</button>
    <button id="btn5">按钮5</button>
    <script>
        const buttons = document.querySelectorAll('button');
        //用 document.querySelectorAll('button') 获取页面中所有的按钮元素
        buttons.forEach(button => {
            //通过 forEach 方法遍历每一个按钮，为每个按钮添加 click 事件监听器。
            button.addEventListener('click', function () {
                buttons.forEach(otherbutton => {
                    otherbutton.classList.remove('dl');
                });
                this.classList.add('dl');
            });
        });
    </script>
</body>

</html>